package algotithm.leetcode.bytedance.interview34;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/19-22:44
 * @URL https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/
 * @Title 面试题34. 二叉树中和为某一值的路径
 */
public class Solution {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    LinkedList<Integer> path = new LinkedList<Integer>();
    LinkedList<List<Integer>> res = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root, target);
        return res;
    }

    public void dfs(TreeNode root,int target) {
        if (root == null) {
            return;
        }
        path.add(root.val);
        target -= root.val;
        if (target == 0 &&  root.left == null && root.right == null) {
            res.add(new ArrayList<>(path));
        }
        dfs(root.left,target);
        dfs(root.right,target);
        path.removeLast();
    }
}
